package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.RechargeLog;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.ShareAccount;
import com.vcc.data.entity.ShareAccountOrder;
import com.vcc.data.mapper.ShareAccountMapper;
import com.vcc.service.boss.share.account.response.BossShareAccountResponse;
import com.vcc.service.boss.share.order.response.BossShareAccountOrderResponse;
import com.vcc.service.client.share.account.response.ClientShareAccountResponse;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.ShareAccountService;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.RechargeLogTableDef.RECHARGE_LOG;
import static com.vcc.data.entity.table.ShareAccountOrderTableDef.SHARE_ACCOUNT_ORDER;
import static com.vcc.data.entity.table.ShareAccountTableDef.SHARE_ACCOUNT;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class ShareAccountServiceImpl extends ServiceImpl<ShareAccountMapper, ShareAccount> implements ShareAccountService {
    @Override
    public Page<ClientShareAccountResponse> paging(Integer pageNumber, Integer pageSize, Role role, Long userId, Long targetUserId, String name, Integer status) {
        QueryChain<ShareAccount> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT.ALL_COLUMNS);
        queryChain.select(USER.EMAIL.as("account"));
        queryChain.leftJoin(USER).on(SHARE_ACCOUNT.USER_ID.eq(USER.ID));
        if (role.getHasAllData()) {
            queryChain.where(
                    USER.PARENT_ID.eq(userId).or(USER.ID.eq(userId))
            );
            if (RegexUtil.checkObjectIsNotNull(targetUserId)) {
                queryChain.and(SHARE_ACCOUNT.USER_ID.eq(targetUserId));
            }
        } else {
            queryChain.where(SHARE_ACCOUNT.USER_ID.eq(userId));
        }
        if (RegexUtil.checkStringIsNotNull(name)) {
            queryChain.and(SHARE_ACCOUNT.ACCOUNT_NAME.like(name ));
        }
        queryChain.orderBy(SHARE_ACCOUNT.CREATE_DATE.desc());
        return queryChain.pageAs(
                Page.of(
                        pageNumber == null ? 1 : pageNumber,
                        pageSize == null ? 20 : pageSize
                ),
                ClientShareAccountResponse.class
        );
    }

    @Override
    public List<ClientShareAccountResponse> list(Long userId, String channelCode) {
        QueryChain<ShareAccount> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT.ALL_COLUMNS);
        queryChain.select(USER.EMAIL.as("account"));
        queryChain.leftJoin(USER).on(SHARE_ACCOUNT.USER_ID.eq(USER.ID));
        queryChain.where(SHARE_ACCOUNT.USER_ID.eq(userId));
        if (RegexUtil.checkStringIsNotNull(channelCode)) {
            queryChain.and(SHARE_ACCOUNT.CHANNEL_CODE.eq(channelCode));
        }
        return queryChain.listAs(ClientShareAccountResponse.class);
    }

    @Override
    public Page<BossShareAccountResponse> paging(Integer pageNumber, Integer pageSize, Long targetUserId, String name, Integer status) {
        QueryChain<ShareAccount> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT.ALL_COLUMNS);
        queryChain.select(USER.EMAIL.as("account"));
        queryChain.select(USER.EMAIL.as("parentAccount"));
        queryChain.select(BOSS_USER.as("dealer").EMAIL.as("dealerAccount"));
        queryChain.leftJoin(USER).on(SHARE_ACCOUNT.USER_ID.eq(USER.ID));
        queryChain.leftJoin(USER.as("parent")).on(USER.PARENT_ID.eq(USER.as("parent").ID));
        queryChain.leftJoin(BOSS_USER.as("dealer")).on(BOSS_USER.ID.eq(USER.BE_LONG_TO_DEALER));
        if (!BossUserUtil.hasAllData()) {
            Long bossUserId = BossUserUtil.getId();
            queryChain.and(
                    BOSS_USER.as("dealer").ID.eq(bossUserId)
                            .or(BOSS_USER.as("dealer").PARENT_ID.eq(bossUserId))
            );
        }
        if (targetUserId != null) {
            queryChain.where(SHARE_ACCOUNT.USER_ID.eq(targetUserId));
        }
        if (RegexUtil.checkStringIsNotNull(name)) {
            queryChain.and(SHARE_ACCOUNT.ACCOUNT_NAME.like(name));
        }
        queryChain.orderBy(SHARE_ACCOUNT.CREATE_DATE.desc());
        return queryChain.pageAs(
                Page.of(
                        pageNumber == null ? 1 : pageNumber,
                        pageSize == null ? 20 : pageSize
                ),
                BossShareAccountResponse.class
        );
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        return null;
    }

    @Override
    public Boolean updateName(Long id, String name) {
        return updateChain()
                .set(SHARE_ACCOUNT.ACCOUNT_NAME, name)
                .where(SHARE_ACCOUNT.ID.eq(id))
                .update();
    }

    @Override
    public Boolean updateUserId(Long id, Long userId) {
        return updateChain()
                .set(SHARE_ACCOUNT.USER_ID, userId)
                .where(SHARE_ACCOUNT.ID.eq(id))
                .update();
    }

    @Override
    public Long countByUserId(Long userId) {
        return queryChain()
                .where(SHARE_ACCOUNT.USER_ID.eq(userId))
                .count();
    }
}
